package com.yan.flower.controller;

import com.yan.flower.domain.FlowerOrder;
import com.yan.flower.service.FlowerService;

import java.util.Scanner;

public class FlowerIndex {
    //public static final Scanner INPUT = new Scanner(System.in);

    //准备存放鲜花数组
    public static final FlowerOrder[] orderList = new FlowerOrder[5];

    public static int COUNT = orderList.length;

    static {
        orderList[0] = new FlowerOrder("1001", "金色海岸-香槟玫瑰", 10, 318.0, "2016-12-12", "胡培元", "");
        orderList[1] = new FlowerOrder("1002", "初心永不变-白玫瑰", 99, 538.0, "2016-12-20", "张发荣", "");
        orderList[2] = new FlowerOrder("1003", "梦里花开-紫色桔梗", 40, 148.0, "2016-12-24", "愿意梦", "烟波...");
        orderList[3] = new FlowerOrder("1004", "心花怒放-粉香百合", 120, 188.0, "2016-12-26", "样林峰", "内层...");
        orderList[4] = new FlowerOrder("1005", "执青春-无声的爱  ", 1000, 219.0, "2016-12-18", "张秋生", "我爱李...");
    }
    public static final Scanner INPUT = new Scanner(System.in);
    
    public static final  FlowerService flowerService = new FlowerService() {
        @Override
        public void findAll() {
            System.out.println("编号\t鲜花名称\t\t\t\t销售数量\t价格\t销售日期\t销售员\t备注");
            for (int i = 0; i < COUNT; i++) {
                System.out.println("" + orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNum() + "\t\t" +  orderList[i].getPrice() + "\t" +  orderList[i].getDate() + "\t" +  orderList[i].getSalesMan() + "\t" +  orderList[i].getRemark() + "");
            }
            System.out.println();
            System.out.println();
        }

        @Override
        public void update() {
            System.out.println("输入销售编号");
            String uid = INPUT.next();
            boolean flag = false;
            for (FlowerOrder order :  orderList) {
                if (order.getId().equals(uid)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                for (int i = 0; i <  orderList.length; i++) {
                    if ( orderList[i].getId().equals(uid)) {
                        System.out.println("您要修改的订单信息如下");
                        System.out.println();
                        System.out.println("编号\t鲜花名称\t\t\t\t销售数量\t价格\t销售日期\t销售员\t备注");
                        System.out.println("" +  orderList[i].getId() + "\t" +  orderList[i].getName() + "\t" +  orderList[i].getNum() + "\t\t" +  orderList[i].getPrice() + "\t" +  orderList[i].getDate() + "\t" +  orderList[i].getSalesMan() + "\t" +  orderList[i].getRemark() + "");
                        System.out.println("请输入修改后的鲜花名称");
                        String newName = INPUT.next();
                        System.out.println("请输入修改后的销售数量");
                        int newNum = INPUT.nextInt();
                        System.out.println("请输入修改后的鲜花价格");
                        int newPrice = INPUT.nextInt();
                        System.out.println("请输入修改后的销售日期(yyyy-MM-dd)");
                        String newDate = INPUT.next();
                        System.out.println("请输入修改后的销售员");
                        String newSale = INPUT.next();
                        System.out.println("请输入修改后的备注");
                        String newRemark = INPUT.next();
                        System.out.println("您确定要修改吗（y/n）");
                        String flag1 = INPUT.next();
                        if ("y".equals(flag1)) {
                            orderList[i] = new FlowerOrder(uid, newName, newNum, newPrice, newDate, newSale, newRemark);
                            System.out.println("修改成功");
                            break;
                        }
                    }
                }

            } else {
                System.out.println("对不起,您输入的id不正确");
            }
        }

        @Override
        public void delete() {
            System.out.println("请输入要删出的订单号");
            String uid = INPUT.next();
            boolean flag = false;
            for (FlowerOrder order :  orderList) {
                if (order.getId().equals(uid)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                for (int i = 0; i <  orderList.length; i++) {
                    if ( orderList[i].getId().equals(uid)) {
                        System.out.println("您要删除的订单信息如下");
                        System.out.println();
                        System.out.println("编号\t鲜花名称\t\t\t\t销售数量\t价格\t销售日期\t销售员\t备注");
                        System.out.println("" +  orderList[i].getId() + "\t" +  orderList[i].getName() + "\t" +  orderList[i].getNum() + "\t\t" +  orderList[i].getPrice() + "\t" +  orderList[i].getDate() + "\t" +  orderList[i].getSalesMan() + "\t" +  orderList[i].getRemark() + "");
                        System.out.println("您确定要删除吗?(y/n)");
                        String flag1 = INPUT.next();
                        if ("y".equals(flag1)) {
                            for (int j = i; j < COUNT - 1; j++) {
                                orderList[j] =  orderList[j + 1];
                            }
                        }
                    }
                }
                System.out.println("删除成功！");
                COUNT--;
            } else {
                System.out.println("对不起,您输入的编号不正确");
            }
        }
    };


    public static void main(String[] args) {
        outer:
        while (true) {
            System.out.println("=====================欢迎光临\"七彩鲜花\"销售管理系统=========================");
            System.out.println("1.查询销售订单");
            System.out.println("2.修改销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.系统退出");
            System.out.println("==========================================================================");
            int choice;
            choice = INPUT.nextInt();
            switch (choice) {
                case 1: // 查询订单
                    flowerService.findAll();
                    break;
                case 2:
                    flowerService.update();
                    break;
                case 3:
                    flowerService.delete();
                    break;
                default:
                    System.err.println("系统退出");
                    break outer;
            }
        }
    }
}
